import { BaseModel, sequelize, Sequelize } from "./basemodel.js";

class Studentgradesummary extends BaseModel {
	static init() {
		return super.init(
			{
				summary_id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
				student_id: { type: Sequelize.INTEGER, allowNull: false },
				semester_name: { 
					type: Sequelize.STRING(20), 
					allowNull: false,
					comment: '如: 2020/2-2020/7' 
				},
				data_hash: { 
					type: Sequelize.STRING(66), 
					comment: '成绩数据的哈希值(SHA-256)' 
				},
				transaction_hash: { 
					type: Sequelize.STRING(66),
					comment: '区块链交易哈希' 
				},
				onchain_status: {
					type: Sequelize.ENUM('not_on_chain', 'pending', 'confirmed'),
					defaultValue: 'not_on_chain',
					comment: '状态: not_on_chain/pending/confirmed'
				},
				onchain_time: { 
					type: Sequelize.DATE,
					comment: '上链时间' 
				},
				cid: { 
					type: Sequelize.STRING(255),
					comment: 'IPFS cid' 
				},
				registrar_sig: { 
					type: Sequelize.STRING(255),
					comment: '教务以太坊签名' 
				},
				student_sig: { 
					type: Sequelize.STRING(255),
					comment: '学生以太坊签名' 
				},
				pdf_path: {
					type: Sequelize.STRING(255),
					comment: 'pdf文件路径'
				},
				processing_status: {
					type: Sequelize.ENUM('pending', 'processing', 'completed', 'failed'),
					defaultValue: 'pending'
				},
				registrar_address:{
					type:Sequelize.STRING(255),
					comment: '教务以太坊地址'
				},
				student_address: { 
					type: Sequelize.STRING(255),
					comment: '学生以太坊签名' 
				},
				batch_hash:{
					type: Sequelize.STRING(255),
					comment: '批次哈希'
				},
				confirm_status:{
					type: Sequelize.ENUM('not_confirm', 'confirming', 'confirmed'),
					defaultValue: 'not_confirm'
				}
			}, 
			{ 
				sequelize,
				tableName: "studentgradesummary",
				modelName: "studentgradesummary",
				comment: '学生学期成绩总表',
				indexes: [
					{
						name: 'idx_student_semester',
						fields: ['student_id', 'semester_name']
					},
					{
						name: 'idx_data_hash',
						fields: ['data_hash']
					},
					{
						name: 'idx_transaction_hash',
						fields: ['transaction_hash']
					}
				]
			}
		);
	}
	
	static listFields() {
		return [
			'summary_id', 
			'student_id', 
			'semester_name', 
			'data_hash', 
			'transaction_hash', 
			'processing_status',
			'onchain_status', 
			'onchain_time',
			'cid',
			'registrar_sig',
			'student_sig',
			'pdf_path',
			'confirm_status'
		];
	}

	static viewFields() {
		return [
			'summary_id', 
			'student_id', 
			'semester_name', 
			'data_hash', 
			'transaction_hash', 
			'onchain_status', 
			'onchain_time',
			'cid',
			'registrar_sig',
			'student_sig'
		];
	}

	static editFields() {
		return [
			'summary_id', 
			'student_id', 
			'semester_name', 
			'data_hash', 
			'transaction_hash', 
			'onchain_status', 
			'onchain_time',
			'cid',
			'registrar_sig',
			'student_sig'
		];
	}
	
	static searchFields(){
		return [
			Sequelize.literal("summary_id LIKE :search"), 
			Sequelize.literal("semester_name LIKE :search"), 
			Sequelize.literal("pdf_path LIKE :search"),
			Sequelize.literal("data_hash LIKE :search"), 
			Sequelize.literal("transaction_hash LIKE :search"), 
			Sequelize.literal("onchain_status LIKE :search"),
		];
	}
}

Studentgradesummary.init();
export default Studentgradesummary;